/*
 * @Author: yushuang
 * @Date: 2024-12-17 17:25:56
 * @LastEditors: yushuang
 * @LastEditTime: 2025-01-06 15:29:55
 * @Description:
 */
import { AxiosError } from 'axios';
import router from '@/router';
import { useMetaTitle } from '@/composables/meta-title';
import { setRouteEmitter } from '@/utils/route-listener';
import { useUserStore } from '@/stores/user';
import { useLoadingCheck, useScrollToTop } from '@/composables/loading';
import { useAuthorization } from '@/composables/authorization';

const allowList = ['/login', '/error', '/401', '/404', '/403'];
const loginPath = '/login';

router.beforeEach(async (to, _, next) => {
  setRouteEmitter(to);
  // 获取
  const userStore = useUserStore();
  const token = useAuthorization();

  if (!token.value) {
    //  如果token不存在就跳转到登录页面
    if (!allowList.includes(to.path) && !to.path.startsWith('/redirect')) {
      next({
        path: loginPath,
        query: {
          redirect: encodeURIComponent(to.fullPath)
        }
      });
      return;
    }
  } else {
    if (!userStore.userInfo && !allowList.includes(to.path) && !to.path.startsWith('/redirect')) {
      try {
        // 获取用户信息
        await userStore.getUserInfo();
        // 获取路由菜单的信息
        const currentRoute = await userStore.generateDynamicRoutes();
        router.addRoute(currentRoute);
        next({
          ...to,
          replace: true
        });
        return;
      } catch (e) {
        if (e instanceof AxiosError && e?.response?.status === 401) {
          // 跳转到error页面
          next({
            path: '/401'
          });
        }
      }
    } else {
      // 如果当前是登录页面就跳转到首页
      if (to.path === loginPath) {
        next({
          path: '/'
        });
        return;
      }
    }
  }
  next();
});

router.afterEach(to => {
  useMetaTitle(to);
  useLoadingCheck();
  useScrollToTop();
});
